
package com.insigma.business.qrcode.soldier.service.impl;


import com.insigma.business.qrcode.common.dao.GovEmployeeJobDao;
import com.insigma.business.qrcode.common.dao.GovOrgDao;
import com.insigma.business.qrcode.common.dto.GovEmployeeJobDO;
import com.insigma.business.qrcode.common.dto.GovOrgBSearcher;
import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.soldier.dao.GovEmployeeDao;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.entity.GovEmployeeDO;
import com.insigma.business.qrcode.soldier.entity.GovOrgDO;
import com.insigma.business.qrcode.soldier.service.ZzdGovOrgService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service("zzdGovOrgService")
public class ZzdGovOrgServiceImpl implements ZzdGovOrgService {

    @Autowired(required = false)
    private GovOrgDao govOrgDao;

    @Autowired(required = false)
    private GovEmployeeDao govEmployeeDao;

    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Autowired(required = false)
    private GovEmployeeJobDao govEmployeeJobDao;

    @Override
    public GovOrgDO get(String id) {
        return null;
    }

    @Override
    public Integer insert(GovOrgDO govOrg) {
        govOrg.setId(sequenceGenerator.genKey(SequenceNameConstant.GOV_ORG_ID));
        return govOrgDao.insertZzd(govOrg);
    }

    @Override
    public Integer update(GovOrgDO govOrg) {
        return null;
    }

    @Override
    public Integer delete(String id, String modifierType, String modifierId) {
        return null;
    }

    @Override
    public Integer insertEmployee(GovEmployeeDO employeeDO) {
        employeeDO.setId(sequenceGenerator.genKey(SequenceNameConstant.GOV_EMPLOYEE_ID));
        return govEmployeeDao.insertZzd(employeeDO);
    }

    @Override
    public Integer insertJob(GovEmployeeJobDO jobDO) {
        jobDO.setId(sequenceGenerator.genKey(SequenceNameConstant.GOV_EMPLOYEE_JOB_ID));
        return govEmployeeJobDao.insertJob(jobDO);
    }

    @Override
    public List<GovEmployeeDO> listEmployeeByAdminAreaCode(String adminAreaCode) {
        List<GovOrgDO> byZzdAreaCode = govOrgDao.getByZzdAreaCode(adminAreaCode);
        if (!CollectionUtils.isEmpty(byZzdAreaCode)) {
            List<String> orgs = byZzdAreaCode.stream().map(s -> s.getId()).collect(Collectors.toList());
            return govEmployeeDao.listZzdEmployeeByOrgId(orgs);
        }
        return null;
    }

    @Override
    public List<GovEmployeeDO> listEmployeeByName(String realName) {
        return govEmployeeDao.listEmployeeByName(realName);
    }

    @Override
    public void realDelAllOriginData(String adminAreaCode) {
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        govEmployeeDao.realDelZzd(areaLike);
        govEmployeeJobDao.realDelZzd(areaLike);
        govOrgDao.realDelZzd(areaLike);
    }

    @Override
    public GovEmployeeDO getEmployeeByOuterAccountId(String zzdId) {
        return govEmployeeDao.getZzdEmployeeByOuterAccountId(zzdId);
    }

    @Override
    public GovEmployeeDO getZzdEmployee(String id) {
        return govEmployeeDao.getZzdEmployee(id);
    }

    @Override
    public List<GovEmployeeDO> getFwzWorkers(String adminAreaCode, String type) {
        BizCheckUtil.checkNotNull(adminAreaCode, "地区码不能为空");
        BizCheckUtil.checkNotNull(type, "type不能为空");
        List<GovEmployeeDO> govEmployeeDOS = listEmployeeByAdminAreaCode(adminAreaCode);
        if (!CollectionUtils.isEmpty(govEmployeeDOS)) {
            govEmployeeDOS = govEmployeeDOS.stream()
                    .filter(s -> "ENABLED".equals(s.getStatus()))
                    .filter(s -> s.getType() != null && s.getType().contains(type))
                    .collect(Collectors.toList());

        }
        return govEmployeeDOS;
    }

    @Override
    public List<GovEmployeeDO> getRootSubFwzWorkers(String rootAdminAreaCode, String soldierAdminAreaCode, String type) {
        int nextCodeLength = AdminAreaCodeUtils.getNextCodeLength(rootAdminAreaCode);
        String fillAreaCode = AdminAreaCodeUtils.fillAreaCode(soldierAdminAreaCode.substring(0, nextCodeLength));
        return getFwzWorkers(fillAreaCode, type);
    }

    @Override
    public List<GovOrgDO> listByAreaCode(String adminAreaCode) {
        return  govOrgDao.getByZzdAreaCode(adminAreaCode);
    }

    @Override
    public Integer insertZzdGov(GovOrgDO govOrg) {
        govOrg.setId(sequenceGenerator.genKey(SequenceNameConstant.GOV_ORG_ID));
        return govOrgDao.insertZzd(govOrg);
    }

    @Override
    public Integer deleteEmployee(String id, String modifierType, String modifierId) {
        return govEmployeeDao.deleteZzd(id, modifierType, modifierId);
    }

    @Override
    public List<GovOrgDO> searchGov(GovOrgBSearcher govOrgBSearcher) {
        if (Strings.isNotBlank(govOrgBSearcher.getAdminAreaCode())) {
            govOrgBSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(govOrgBSearcher.getAdminAreaCode()));
        }
        return govOrgDao.searchGov(govOrgBSearcher);
    }


}
